package ui;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import util.ImagePanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class AboutUI implements MouseListener
	{
		private static AboutUI instance = null;
		
		private MainWindow frameAboutUI;
		
		private JLabel blueBitLabel;
		private JLabel creators;
		private JLabel info;
		private JLabel vaizdineRek;
		private JLabel nikolaLabel;
		private JLabel java;
		private JLabel eclipse;
		private JLabel mssql;
		
		private BufferedImage blueBit = null;
		private BufferedImage nikola = null;
		private BufferedImage vrImg = null;
		private BufferedImage javaImg = null;
		private BufferedImage mssqlImg = null;
		private BufferedImage eclipseImg = null;
		
		private Image image = null;
		private ImagePanel content;
		
		private JButton back;
		private JButton statistics;
		
		private backButton backButt;
				
		public void display()
			{
				frameAboutUI.setSize(700,400);
				frameAboutUI.setVisible(true);
				frameAboutUI.setResizable(false);
				frameAboutUI.setLocationRelativeTo(null);
			}
		public AboutUI()
			{
				frameAboutUI = new MainWindow();
				
				backButt = new backButton();
				backButt.setNumber("6");
				
				statistics = new JButton();
				statistics.setOpaque(false);
				statistics.setFocusPainted(false);
				statistics.setText("Statistics");
				statistics.addMouseListener(this);
				
				back = new JButton();
				back.setOpaque(false);
				back.setFocusPainted(false);
				back.setText("Back");
				back.addMouseListener(this);
				
				try
					{
						image = ImageIO.read(new File("img/background/background.jpg"));
					}
				catch(Exception e)
					{
						e.printStackTrace();
					}
				content = new ImagePanel(image);
				content.setBounds(0, 31, 694, 289);
				
				GroupLayout groupLayout = new GroupLayout(frameAboutUI.getContentPane());
				groupLayout.setHorizontalGroup(
					groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(content, GroupLayout.PREFERRED_SIZE, 694, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(9)
							.addComponent(back, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 472, Short.MAX_VALUE)
							.addComponent(statistics, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
							.addGap(21))
				);
				groupLayout.setVerticalGroup(
					groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(31)
							.addComponent(content, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE)
							.addGap(5)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(back)
								.addComponent(statistics))
							.addContainerGap(23, Short.MAX_VALUE))
				);
				frameAboutUI.getContentPane().setLayout(groupLayout);
				
				try
					{
						vrImg = ImageIO.read(new File("img/about/logo.png"));
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				
				info = new JLabel();
				info.setText("<html><p><strong>Vaizdine Reklama Administration System<strong></p>\r\n<p>\r\n<br>\r\nVersion : 1.0\r\n<br>\r\nOwner : Vaizdine Reklama\r\n<br>\r\nFeatures :\r\n<br>\r\n<ul>\r\n<li>\r\nAdministration on all operations\r\n</li>\r\n<li>\r\nTrack of employees\r\n</li>\r\n<li>\r\nStatistics of operations and customers\r\n</li>\r\n</ul>\r\n</p>\r\n<p>\r\n\u00A9 Copyright 2013 BlueBit & Nikola Hrusov\r\n</p>\r\n</html>\r\n");
				info.setFont(new Font("Dotum", Font.PLAIN, 12));
				
				
				vaizdineRek = new JLabel(new ImageIcon(vrImg));
				vaizdineRek.setBorder(BorderFactory.createLineBorder(Color.black));
				vaizdineRek.addMouseListener(this);
				vaizdineRek.setToolTipText("Vaizdine Reklama");
				creators = new JLabel();
				creators.setText("<html>\r\n<strong>Developed By: </strong>\r\n</html>\r\n");
				
				try
					{
						blueBit = ImageIO.read(new File("img/about/blueBit.png"));
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				
				blueBitLabel = new JLabel(new ImageIcon(blueBit));
				blueBitLabel.setToolTipText("Austeja M Vaicyte & Purcaru Theodor Florian");
				blueBitLabel.setBorder(BorderFactory.createLineBorder(Color.black));
				blueBitLabel.addMouseListener(this);
				try
					{
						nikola = ImageIO.read(new File("img/about/nikola.png"));
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				
				nikolaLabel = new JLabel(new ImageIcon(nikola));
				nikolaLabel.setToolTipText("Nikola Hrusov");
				nikolaLabel.setBorder(BorderFactory.createLineBorder(Color.black));
				nikolaLabel.addMouseListener(this);
				try
					{
						javaImg = ImageIO.read(new File("img/about/java.png"));
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				
				java = new JLabel(new ImageIcon(javaImg));
				java.setToolTipText("www.java.com");
				java.setBorder(BorderFactory.createLineBorder(Color.black));
				java.addMouseListener(this);
				try
					{
						mssqlImg = ImageIO.read(new File("img/about/mssql.png"));
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				
				mssql = new JLabel(new ImageIcon(mssqlImg));
				mssql.setToolTipText("www.microsoft.com");
				mssql.setBorder(BorderFactory.createLineBorder(Color.black));
				mssql.addMouseListener(this);
				try
					{
						eclipseImg = ImageIO.read(new File("img/about/eclipse.png"));
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				
				eclipse = new JLabel(new ImageIcon(eclipseImg));
				eclipse.setToolTipText("www.eclipse.org");
				eclipse.setBorder(BorderFactory.createLineBorder(Color.black));
				eclipse.addMouseListener(this);
				
				JLabel lblsoftwareUsed = new JLabel();
				lblsoftwareUsed.setText("<html>\r\n<strong>Software used: </strong>\r\n</html>\r\n");
				GroupLayout gl_content = new GroupLayout(content);
				gl_content.setHorizontalGroup(
					gl_content.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_content.createSequentialGroup()
							.addGap(19)
							.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_content.createSequentialGroup()
									.addGap(67)
									.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_content.createSequentialGroup()
											.addComponent(nikolaLabel, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
											.addGap(10)
											.addComponent(blueBitLabel, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
										.addComponent(creators, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED, 216, Short.MAX_VALUE)
									.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_content.createSequentialGroup()
											.addComponent(java, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(eclipse, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(mssql, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))
										.addComponent(lblsoftwareUsed, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE))
									.addGap(102))
								.addGroup(gl_content.createSequentialGroup()
									.addComponent(vaizdineRek, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(info, GroupLayout.PREFERRED_SIZE, 292, GroupLayout.PREFERRED_SIZE)
									.addContainerGap(314, Short.MAX_VALUE))))
				);
				gl_content.setVerticalGroup(
					gl_content.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_content.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_content.createSequentialGroup()
									.addGap(5)
									.addComponent(vaizdineRek, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
								.addComponent(info, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE))
							.addGap(14)
							.addGroup(gl_content.createParallelGroup(Alignment.BASELINE)
								.addComponent(creators)
								.addComponent(lblsoftwareUsed))
							.addGap(11)
							.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_content.createSequentialGroup()
									.addGroup(gl_content.createParallelGroup(Alignment.TRAILING)
										.addComponent(nikolaLabel, GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
										.addComponent(blueBitLabel, GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
									.addGap(32))
								.addGroup(gl_content.createSequentialGroup()
									.addGroup(gl_content.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(mssql, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(java, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(eclipse, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))
									.addContainerGap(28, Short.MAX_VALUE))))
				);
				content.setLayout(gl_content);
			}
		public static AboutUI getInstance()
			{
				if(instance == null)
					instance = new AboutUI();
				
				return instance;
			}
		
		public static void openWebpage(String urlString) {
		    try {
		        Desktop.getDesktop().browse(new URL(urlString).toURI());
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
		}
		
		public static void main(String [] args)
			{
				try
					{
						UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
						
						AboutUI about = new AboutUI();
						about.display();
					}
				catch(Exception e)
					{
						JOptionPane.showMessageDialog(null, "About could not load", "Error", JOptionPane.ERROR_MESSAGE);
					}
			}
		@Override
		public void mouseClicked(MouseEvent e)
			{
				if(e.getSource() == statistics)
					{
						StatisticsUI stats = StatisticsUI.getInstance();
						stats.display();
						frameAboutUI.setVisible(false);
					}
				if(e.getSource() == back)
					{
						backButt.goBack(backButt.getNumber());
						frameAboutUI.setVisible(false);
					}
				if(e.getSource() == blueBitLabel)
					{
						openWebpage("https://www.facebook.com/greenbluebit");
					}
				if(e.getSource() == nikolaLabel)
					{
						openWebpage("https://www.facebook.com/nikola.hrusov");
					}
				if(e.getSource() == java)
					{
						openWebpage("http://www.java.com");
					}
				if(e.getSource() == mssql)
					{
						openWebpage("http://www.microsoft.com/en-us/sqlserver/default.aspx");
					}
				if(e.getSource() == eclipse)
					{
						openWebpage("http://www.eclipse.org");
					}
				if(e.getSource() == vaizdineRek)
					{
						openWebpage("http://www.vaizdinereklama.lt");
					}
			}
		@Override
		public void mouseEntered(MouseEvent e)
			{
				if(e.getSource() == blueBitLabel)
					{
						try
						       {
						        BufferedImage blueBitSec = ImageIO.read(new File("img/about/blueBit-hover.png"));
						        blueBitLabel.setIcon(new ImageIcon(blueBitSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == nikolaLabel)
					{
						try
						       {
						        BufferedImage nikolaSec = ImageIO.read(new File("img/about/nikola-hover.png"));
						        nikolaLabel.setIcon(new ImageIcon(nikolaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == java)
					{
						try
						       {
						        BufferedImage javaSec = ImageIO.read(new File("img/about/java-hover.png"));
						        java.setIcon(new ImageIcon(javaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == mssql)
					{
						try
						       {
						        BufferedImage mssqlSec = ImageIO.read(new File("img/about/mssql-hover.png"));
						        mssql.setIcon(new ImageIcon(mssqlSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == eclipse)
					{
						try
						       {
						        BufferedImage eclipseSec = ImageIO.read(new File("img/about/eclipse-hover.png"));
						        eclipse.setIcon(new ImageIcon(eclipseSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == vaizdineRek)
					{
						try
						       {
						        BufferedImage vaizdineSec = ImageIO.read(new File("img/about/logo-hover.png"));
						        vaizdineRek.setIcon(new ImageIcon(vaizdineSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
			}
		@Override
		public void mouseExited(MouseEvent e)
			{
				if(e.getSource() == blueBitLabel)
					{
						try
						       {
						        BufferedImage blueBitSec = ImageIO.read(new File("img/about/blueBit.png"));
						        blueBitLabel.setIcon(new ImageIcon(blueBitSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == nikolaLabel)
					{
						try
						       {
						        BufferedImage nikolaSec = ImageIO.read(new File("img/about/nikola.png"));
						        nikolaLabel.setIcon(new ImageIcon(nikolaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == java)
					{
						try
						       {
						        BufferedImage javaSec = ImageIO.read(new File("img/about/java.png"));
						        java.setIcon(new ImageIcon(javaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == mssql)
					{
						try
						       {
						        BufferedImage mssqlSec = ImageIO.read(new File("img/about/mssql.png"));
						        mssql.setIcon(new ImageIcon(mssqlSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == eclipse)
					{
						try
						       {
						        BufferedImage eclipseSec = ImageIO.read(new File("img/about/eclipse.png"));
						        eclipse.setIcon(new ImageIcon(eclipseSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == vaizdineRek)
					{
						try
						       {
						        BufferedImage vaizdineSec = ImageIO.read(new File("img/about/logo.png"));
						        vaizdineRek.setIcon(new ImageIcon(vaizdineSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				
			}
		@Override
		public void mousePressed(MouseEvent e)
			{
				if(e.getSource() == blueBitLabel)
					{
						try
						       {
						        BufferedImage blueBitSec = ImageIO.read(new File("img/about/blueBit-click.png"));
						        blueBitLabel.setIcon(new ImageIcon(blueBitSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == nikolaLabel)
					{
						try
						       {
						        BufferedImage nikolaSec = ImageIO.read(new File("img/about/nikola-click.png"));
						        nikolaLabel.setIcon(new ImageIcon(nikolaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == java)
					{
						try
						       {
						        BufferedImage javaSec = ImageIO.read(new File("img/about/java-click.png"));
						        java.setIcon(new ImageIcon(javaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == mssql)
					{
						try
						       {
						        BufferedImage mssqlSec = ImageIO.read(new File("img/about/mssql-click.png"));
						        mssql.setIcon(new ImageIcon(mssqlSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == eclipse)
					{
						try
						       {
						        BufferedImage eclipseSec = ImageIO.read(new File("img/about/eclipse-click.png"));
						        eclipse.setIcon(new ImageIcon(eclipseSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == vaizdineRek)
					{
						try
						       {
						        BufferedImage vaizdineSec = ImageIO.read(new File("img/about/logo-click.png"));
						        vaizdineRek.setIcon(new ImageIcon(vaizdineSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				
			}
		@Override
		public void mouseReleased(MouseEvent e)
			{
				if(e.getSource() == blueBitLabel)
					{
						try
						       {
						        BufferedImage blueBitSec = ImageIO.read(new File("img/about/blueBit-hover.png"));
						        blueBitLabel.setIcon(new ImageIcon(blueBitSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == nikolaLabel)
					{
						try
						       {
						        BufferedImage nikolaSec = ImageIO.read(new File("img/about/nikola-hover.png"));
						        nikolaLabel.setIcon(new ImageIcon(nikolaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == java)
					{
						try
						       {
						        BufferedImage javaSec = ImageIO.read(new File("img/about/java-hover.png"));
						        java.setIcon(new ImageIcon(javaSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == mssql)
					{
						try
						       {
						        BufferedImage mssqlSec = ImageIO.read(new File("img/about/mssql-hover.png"));
						        mssql.setIcon(new ImageIcon(mssqlSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == eclipse)
					{
						try
						       {
						        BufferedImage eclipseSec = ImageIO.read(new File("img/about/eclipse-hover.png"));
						        eclipse.setIcon(new ImageIcon(eclipseSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
				if(e.getSource() == vaizdineRek)
					{
						try
						       {
						        BufferedImage vaizdineSec = ImageIO.read(new File("img/about/logo-hover.png"));
						        vaizdineRek.setIcon(new ImageIcon(vaizdineSec));
						       }
						      catch(Exception m)
						       {
						        m.printStackTrace();
						       }
					}
			}
	}
